<?php
/**
 * 流转订单列表(供机构查看本机构的所有流转订单)
 */
use app\models\Manager;
use yii\helpers\Url;
$currentInstitutionCode = Manager::getCurrentManager()['institution_code'];
$web = Url::base();
?>

<div class="panel panel-default" style="display: none;">
    <div class="panel-body">
        <div class="form-inline">
            <div class="btn-group">
                <button id="btnBatchApprove" class="btn btn-primary" disabled type="button" data-toggle="modal"><i
                        class="fa fa-edit"></i> 批量审核订单
                </button>
            </div>
        </div>
    </div>
</div>

<div class="panel panel-default">
    <div class="panel-body">
        <div class="form-inline">

            <div class="form-group col-lg-5">
                <div data-date="" class="input-group date form_date">
                    <input type="text" class="form-control" readonly="" size="16" id="start_time"
                           name="start_time" placeholder="开始时间"
                           value="<?= $start_time ?>">
                    <div class="input-group-btn">
                        <button type="button" class="btn btn-default date-reset"><i class="fa fa-times"></i>
                        </button>
                        <button type="button" class="btn btn-default date-set"><i class="fa fa-calendar"></i>
                        </button>
                    </div>
                </div>
                <div data-date="" class="input-group date form_date">
                    <input type="text" class="form-control" readonly="" size="16" id="end_time"
                           name="end_time" placeholder="结束时间"
                           value="<?= $end_time ?>">
                    <div class="input-group-btn">
                        <button type="button" class="btn btn-default date-reset"><i class="fa fa-times"></i>
                        </button>
                        <button type="button" class="btn btn-default date-set"><i class="fa fa-calendar"></i>
                        </button>
                    </div>
                </div>

            </div>


            <div class="form-group col-lg-3">
                <div class="btn btn-default" type="button" id="exportUserIntegral" onclick="exportUser()">
                    <i class="glyphicon glyphicon-download-alt"></i>
                    <a id="export-excel-download" href="javascript:exportExcle()">导出</a>
                </div>
            </div>

            <div class="form-group col-lg-3" style="display: <?=$currentInstitutionCode=='0' ? 'block' : 'none'?>">
                <?=app\widgets\InstitutionHierarchyAutocompleteWidget::widget(['id' => 'searchInstitution', 'currentInstitutionCode' => $current_institution_code, 'defaultText' => '----选择查询机构----'])  ?>
            </div>
            <div class="form-group col-lg-3" style="display: <?=$currentInstitutionCode!='0' ? 'block' : 'none'?>">
                <?=app\widgets\InstitutionSelfChildrenAutocompleteWidget::widget(['id' => 'searchInstitution_origin_code', 'currentInstitutionCode' => $origin_code, 'defaultText' => '----选择查询子机构----']) ?>
            </div>
            <button id="btnSearch" class="btn btn-primary"><i class="fa fa-search"></i> 搜索</button>
        </div>
    </div>
</div>

<div class="panel">
    <div class="panel-body">
        <div>
            <div class="table-responsive">
                <table class="table table-bordered table-hover table-condensed">
                    <thead class="nowrap">
                    <tr style="color: #0000aa;">
                        <th>当前授信额度:￥<?=$accountRecordSum['inst_account']['credit_quota']?></th>
                        <th>当前可用额度:￥<?=$accountRecordSum['inst_account']['balance']+$accountRecordSum['inst_account']['credit_quota']?></th>
                    </tr>
                    <tr>
                        <th>订单总数:<?=$totalCount?></th>
                        <th>上期余额:￥<?=$accountRecordSum['pre_yu']?></th>
                        <th>本期上账:￥<?=$accountRecordSum['now_inc']?></th>
                        <th style="display: none;">订单结算总额:￥<?=$accountRecordSum['now_dec']?></th>
                        <th style="display: none;">>积分结算总额:￥<?=$accountRecordSum['now_dec']+$accountRecordSum['now_pay']?></th>
                        <th style="display: none;">>现金结算总额:￥<?= $accountRecordSum['now_pay']?></th>
                    </tr>
                    <tr>

                        <th>本期余额:￥<?=$accountRecordSum['pre_yu']+$accountRecordSum['now_inc']+$accountRecordSum['now_dec']+$accountRecordSum['now_back']?></th>
                        <th colspan="2">积分消费总额￥<?=abs($accountRecordSum['now_dec'])-abs($accountRecordSum['now_back'])-abs($accountRecordSum['now_pay'])?>&nbsp;&nbsp;=&nbsp;&nbsp;
                            订单总额￥<?=abs($accountRecordSum['now_dec'])?>
                            &nbsp;&nbsp;-&nbsp;&nbsp;退单总额￥<?=abs($accountRecordSum['now_back'])?>&nbsp;&nbsp;-&nbsp;&nbsp;订单现金支付总额￥<?= abs($accountRecordSum['now_pay'])?></th>


                    </tr>
                    <tr>

                        <th style="display: none;">本期消费:￥<?=$accountRecordSum['now_dec']?></th>
                        <th style="display: none;">>订单退款总金额:￥<?=$accountRecordSum['now_back']?></th>
                    </tr>

                    </thead>
                    <tbody>
                    </tbody>
                    </table>
            </div>
        </div>
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-condensed">
                <thead class="nowrap">
                <tr>
                    <th>订单编号</th>
                    <th>下单用户</th>
                    <th>订单结算总金额</th>
                    <th>现金支付金额</th>
                    <th>订单状态</th>
                    <th>下单时间</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <?php
                foreach ($items as $item) {
                    ?>
                    <tr style=" <?=$item['t5']=='退货' ? 'color: red':''?>">
                        <td><?= $item['t1'] ?></td>
                        <td><?= $item['t2'] ?></td>
                        <td>￥<?= $item['t3'] ?></td>
                        <td>￥<?= $item['t4'] ?></td>
                        <td><?= $item['t5'] ?></td>
                        <td><?= $item['t6'] ?></td>
                        <td>
                            <div class="btn-group">
                                <a class="btn btn-link" data-toggle="modal" data-order-id="<?= $item['t1'] ?>"
                                   href="#orderDetailModal">订单详情</a>
                            </div>
                        </td>
                    </tr>
                    <?php
                }
                ?>
                </tbody>
            </table>
        </div>
        <div class="pull-left">
            <?= \app\widgets\LinkPagerExt::widget(['pagination' => $pagination]) ?>
        </div>
    </div>
</div>

<div aria-hidden="true" role="dialog" tabindex="-1" id="orderDetailModal" class="modal fade">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">订单流转信息</h4>
            </div>
            <div class="modal-body" id="order_detail_content">
                正在加载数据....
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<script type="application/javascript">
    $(function () {


        $('#orderDetailModal').on('show.bs.modal', function (e) {
            $("#order_detail_content").html('正在加载数据....');
            var orderFlowId = e.relatedTarget.attributes['data-order-id'].value;
            loadOrderFlowDetailPartial(orderFlowId);
        });


        $('#btnSearch').click(function () {
            var url = window.location.pathname;



            var searchInstitutionCode = $('#searchInstitution').val();
            if (searchInstitutionCode == 0) {
                alert("请选择查询机构!");
                return;
            } else {
                url = setUrlParam(url, 'institution_code', searchInstitutionCode);
            }
            var origin_code= $('#searchInstitution_origin_code').val();
            if( origin_code && origin_code!=searchInstitutionCode){
                url = setUrlParam(url, 'origin_code', origin_code);
            }

            var start_time = $("#start_time").val();
            if (start_time != null && start_time != '') {
                url = setUrlParam(url, 'start_time', start_time);
            }
            var end_time = $("#end_time").val();
            if (end_time != null && end_time != '') {
                url = setUrlParam(url, 'end_time', end_time);
            }



            url = setUrlParam(url, 'page', 1);
            window.location.href = url;
        });
    });
    function exportExcle(){
        var url='export-flow-list';
        var searchInstitutionCode = $('#searchInstitution').val();
        if (searchInstitutionCode == 0) {
            alert("请选择查询机构!");
            return;
        } else {
            url = setUrlParam(url, 'institution_code', searchInstitutionCode);
        }
        var origin_code= $('#searchInstitution_origin_code').val();
        if( origin_code && origin_code!=searchInstitutionCode){
            url = setUrlParam(url, 'origin_code', origin_code);
        }
        var start_time = $("#start_time").val();
        if (start_time != null && start_time != '') {
            url = setUrlParam(url, 'start_time', start_time);
        }
        var end_time = $("#end_time").val();
        if (end_time != null && end_time != '') {
            url = setUrlParam(url, 'end_time', end_time);
        }



        url = setUrlParam(url, 'page', 1);
        window.open(url);
    }

    function loadOrderFlowDetailPartial(order_code) {
        requestHtml('order-flow-detail-partial', {order_code: order_code,institution_code:$('#searchInstitution').val()}, true, function (responseHtml) {
            $("#order_detail_content").html(responseHtml);
        });
    }


</script>


<link rel="stylesheet" type="text/css" href="<?= $web ?>/css/datetimepicker-custom.css"/>
<script src="<?= $web ?>/js/bootstrap-datetimepicker.js"></script>
<script src="<?= $web ?>/js/pickers-init.js"></script>